package com.dq.mail.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dq.mail.entity.User;
import com.dq.mail.exception.CommonException;
import com.dq.mail.service.UserService;
import com.dq.mail.utils.R;
import com.dq.mail.utils.SystemCommons;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * User-API 用户信息控制层
 *
 * @author Dq
 * @since 2023-03-03 11:04:32
 */
@Api(value = "用户管理",tags = "用户管理")
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation(value = "获取用户列表")
    @GetMapping("userListTest")
    public R getBasicInfo() {
        List<User> list = userService.list();
        return R.ok().data("userList", list);
    }

    /**
     * 用户登录
     *
     * @param user 用户Dto
     * @return 返回token 令牌(Redis 实时刷新)
     */
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public R loginUser(@RequestBody User user) {
        String token = userService.login(user);
        return R.ok().data("token", token);
    }


    /**
     * 用户注册
     *
     * @param userDto 用户DTO
     * @return 返回code作为判断
     */
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public R registerUser(@RequestBody User userDto) {
        if (StringUtils.isEmpty(userDto.getUsername()) && StringUtils.isEmpty(userDto.getPassword())) {
            return R.error().message("传入参数错误 请仔细检查");
        }
        User one = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, userDto.getUsername()));
        if (ObjectUtils.isEmpty(one)) {
            userDto.setPassword(DigestUtils.md5Hex(userDto.getPassword() + SystemCommons.md5Secret));
            userService.save(userDto);
            return R.ok().message("注册成功");
        } else {
            return R.error().message("已有人注册此账户，请重新输入账户名");
        }
    }

    /**
     * 获取用户基本信息
     *
     * @param token
     * @return
     */
    @ApiOperation("获取用户基本信息")
    @GetMapping()
    public R getUserInfo(@RequestHeader(value = "authorization") String token) {
        User user = userService.getUserInfoByToken(token);
        if (ObjectUtils.isEmpty(user)) {
            return R.error().message("用户状态已过期 请重新登录").code(2003);
        }
        return R.ok().data("userBasicInfo", user);
    }

    /**
     * 修改用户密码 PUT请求
     * @param user
     * @return
     */
    @ApiOperation("修改用户密码")
    @PutMapping
    public R changeUserPass(@RequestBody User user){
        if (ObjectUtils.isEmpty(user)){
            CommonException.throwError("修改失败 请联系管理人员[Empty Object]");
        }
        user.setPassword(DigestUtils.md5Hex(user.getPassword()+SystemCommons.md5Secret));
        userService.updateById(user);
        return R.ok();
    }
}